#include "widget.h"
#include "ui_widget.h"
#include <time.h>
bool isonly(int num[9][9])//用来判断是否有唯一解的函数
{
    int y = 0, c, b, r, x, k, l, j, i, z;
    int sumr[9][10], sumc[9][10], sumb[9][10], sumbr[9][9][10], sumbc[9][9][10];
    int check[9][9], sum[9][10], feature[9][9][10];
    for (i = 0;i < 9;i++)
    {
        for (j = 0;j < 9;j++)
            check[i][j] = num[i][j];
    }
    for (x = 1;x <= 60;x++)
    {
        for (i = 0;i < 9;i++)
        {
            for (j = 0; j <= 9;j++)
            {

                sum[i][j] = sumr[i][j] = sumc[i][j] = sumb[i][j] = 0;//将数组置零
            }
        }
        for (i = 0;i < 9;i++)
        {
            for (j = 0; j < 9;j++)
            {
                for (y = 1; y <= 9;y++)
                {
                    sumbr[i][j][y] = sumbc[i][j][y] = 0;//将数组置零
                }
            }
        }
        for (i = 0;i < 9;i++)
        {
            for (j = 0;j < 9;j++)
            {
                switch (check[i][j])
                {
                case 0:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 1;
                    break;
                case 1:feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][1] = 1;
                    break;
                case 2:feature[i][j][1] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][2] = 1;
                    break;
                case 3:feature[i][j][1] = feature[i][j][2] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][3] = 1;
                    break;
                case 4:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][4] = 1;
                    break;
                case 5:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][5] = 1;
                    break;
                case 6:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][6] = 1;
                    break;
                case 7:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][8] = feature[i][j][9] = 0;
                    feature[i][j][7] = 1;
                    break;
                case 8:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][9] = 0;
                    feature[i][j][8] = 1;
                    break;
                case 9:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = 0;
                    feature[i][j][9] = 1;
                    break;
                }//定义特征值
            }
        }

        for (i = 0;i < 9;i++)
        {
            for (j = 0;j < 9;j++)
            {
                if (check[i][j] == 0)
                {
                    for (r = 0; r < 9; r++)
                    {
                        for (y = 1;y <= 9;y++)
                        {
                            if (check[r][j] == y)
                            {
                                feature[i][j][y] = 0;
                            }
                        }
                    }
                    for (c = 0; c < 9; c++)
                    {
                        for (y = 1;y <= 9;y++)
                        {
                            if (check[i][c] == y)
                            {
                                feature[i][j][y] = 0;
                            }
                        }
                    }
                    for (k = ((int(i / 3)) * 3);k <= int((i / 3)) * 3 + 2;k++)
                    {
                        for (l = (int(j / 3)) * 3;l <= (int(j / 3)) * 3 + 2;l++)
                        {
                            for (y = 1;y <= 9;y++)
                            {
                                if (check[k][l] == y)
                                {
                                    feature[i][j][y] = 0;
                                }
                            }
                        }
                    }
                }
            }
        }//根据行列数字修改特征值
        for (b = 0;b <= 8;b++)
        {
            for (y = 1;y <= 9;y++)
            {
                for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                {
                    for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                    {
                        sumbc[b][j][y] += feature[i][j][y];
                        sumbr[b][i][y] += feature[i][j][y];
                        sumb[b][y] += feature[i][j][y];
                    }

                }
            }
        }//计算每宫中特征值的和
        for (b = 0;b <= 8;b++)
        {
            for (y = 1;y <= 9;y++)
            {

                for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                {
                    for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                    {
                        if (feature[i][j][y] == 1 && (sumb[b][y] == 1))
                        {
                            check[i][j] = y;
                        }
                        if (sumbc[b][j][y] == sumb[b][y] && sumb[b][y] != 0 && sumbc[b][j][y] != 0)
                        {
                            for (r = 0;r < 9;r++)
                            {
                                if (r != (b / 3) * 3 && r != (b / 3) * 3 + 1 && r != (b / 3) * 3 + 2)
                                {
                                    feature[r][j][y] = 0;
                                }
                            }
                        }
                        if (sumbr[b][i][y] == sumb[b][y] && sumb[b][y] != 0 && sumbr[b][i][y] != 0)
                        {
                            for (c = 0;c < 9;c++)
                            {
                                if (c != (b % 3) * 3 && c != (b % 3) * 3 + 1 && c != (b % 3) * 3 + 2)
                                {
                                    feature[i][c][y] = 0;
                                }
                            }
                        }
                    }
                }


            }//根据宫摒除的结果更新特征值
        }//一次宫摒除法*/
        for (i = 0;i < 9;i++)
        {
            for (j = 0;j < 9;j++)
            {
                for (y = 1;y <= 9;y++)
                    sum[i][j] += feature[i][j][y];
            }
        }
        for (i = 0;i < 9;i++)
        {
            for (j = 0;j < 9;j++)
            {
                if (check[i][j] == 0)
                {
                    if (sum[i][j] == 1)
                    {
                        for (y = 1;y <= 9;y++)
                            check[i][j] += y * feature[i][j][y];
                    }
                }
            }
        }//唯一余数法
        for (i = 0;i < 9;i++)
        {
            for (y = 1;y <= 9;y++)
            {
                for (j = 0;j < 9;j++)
                {
                    sumr[i][y] += feature[i][j][y];
                }
            }
        }
        for (i = 0;i < 9;i++)
        {
            for (y = 1;y <= 9;y++)
            {
                if (sumr[i][y] == 1)
                {
                    for (j = 0;j < 9;j++)
                    {
                        if (feature[i][j][y] == 1)
                        {
                            {check[i][j] = y;}
                        }
                    }
                }
            }
        }//行摒除法
        for (j = 0;j < 9;j++)
        {
            for (y = 1;y <= 9;y++)
            {
                for (i = 0;i < 9;i++)
                {
                    sumr[j][y] += feature[i][j][y];
                }
            }
        }
        for (j = 0;j < 9;j++)
        {
            for (y = 1;y <= 9;y++)
            {
                if (sumr[j][y] == 1)
                {
                    for (i = 0;i < 9;i++)
                    {
                        if (feature[i][j][y] == 1)
                        {
                            {check[i][j] = y;}
                        }
                    }
                }
            }
        }//列摒除法
        for (b = 0;b <= 8;b++)
        {
            for (y = 1;y <= 9;y++)
            {
                for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                {
                    for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                    {

                        sumb[b][y] += feature[i][j][y];
                    }
                }
            }
        }
        for (b = 0;b <= 8;b++)
        {
            for (y = 1;y <= 9;y++)
            {
                if (sumb[b][y] == 1)
                {
                    for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                    {
                        for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                        {
                            if (feature[i][j][y] == 1)
                            {
                                check[i][j] = y;
                            }

                        }
                    }
                }
            }
        }//二次宫摒除法

    }



    z = 1;
    for (i = 0;i < 9;i++)
    {
        for (j = 0; j < 9;j++)
        {
            if (check[i][j] == 0)
                z = 0;
        }
    }
    return z;
}

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("数独游戏"));
    this->setFixedSize(this->size());
    for(int row=0; row<9; row++)
        for(int col=0; col<9; col++)
        {
            if(row/3 %2 == col/3%2)
                _btn[row][col].setStyleSheet("background-color: rgb(0,191,255)");
            else
                _btn[row][col].setStyleSheet("background-color: rgb(205,85,85)");//设置颜色
            _btn[row][col].setFixedSize(50,50);
            QFont font(nullptr,40);
            _btn[row][col].setFont(font);
            _btn[row][col].setText("");
            ui->gridLayout->addWidget(&_btn[row][col],row,col);

            connect(&_btn[row][col],&QPushButton::clicked,[this,row,col](){
               QPoint point = this->cursor().pos();//获取当前光标的位置
                Form* form = new Form(point,0,this,row,col);//创建一个小窗口
                form->exec();
            });
        }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::process(bool show)
{
    int num[9][9], flag[81], backarr[81][2],answer[9][9];
    int i, r,w,c, rend, cend, back, layer, row, col, count;

        for (r = 0; r < 9;r++ )
            for (c = 0; c < 9; c++)
                num[r][c] = 0;
        for (i = 0; i < 81; i++)
                flag[i] = 0;
        for (r = 0; r < 81; r++)
            for (c = 0; c < 2; c++)
                backarr[r][c] = 0;
        i = r = c = rend = cend = back = layer = row = col = count = 0;//把所有的数据与变量清零
        if(show) //如果是解答局面的调用
              {
                  for(int i=0; i<81; i++)
                  {
                      int value = _btn[i/9][i%9].text().toInt();
                      if(value)
                      {
                          num[i/9][i%9] = value;
                          flag[i] = 1;
                      }
                  }
                  int y = 0, c,b,r, x, k, l, j, i;
                      int sumr[9][10], sumc[9][10], sumb[9][10],sumbr[9][9][10],sumbc[9][9][10];
                      int check[9][9], sum[9][10], feature[9][9][10];

                      for (i = 0;i < 9;i++)
                      {
                          for (j = 0;j < 9;j++)
                              check[i][j] = num[i][j];
                      }
                      for (x = 1;x <= 60;x++)
                      {
                          for (i = 0;i < 9;i++)
                          {
                              for (j = 0; j <= 9;j++)
                              {

                                  sum[i][j] = sumr[i][j] = sumc[i][j] = sumb[i][j] = 0;
                              }
                          }
                          for (i = 0;i < 9;i++)
                          {
                              for (j = 0; j < 9;j++)
                              {
                                  for (y = 1; y <= 9;y++)
                                  {
                                      sumbr[i][j][y] = sumbc[i][j][y] = 0;
                                  }
                              }
                          }
                          for (i = 0;i < 9;i++)
                          {
                              for (j = 0;j < 9;j++)
                              {
                                  switch (check[i][j])
                                  {
                                  case 0:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 1;
                                      break;
                                  case 1:feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][1] = 1;
                                      break;
                                  case 2:feature[i][j][1] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][2] = 1;
                                      break;
                                  case 3:feature[i][j][1] = feature[i][j][2] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][3] = 1;
                                      break;
                                  case 4:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][4] = 1;
                                      break;
                                  case 5:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][5] = 1;
                                      break;
                                  case 6:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][7] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][6] = 1;
                                      break;
                                  case 7:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][8] = feature[i][j][9] = 0;
                                      feature[i][j][7] = 1;
                                      break;
                                  case 8:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][9] = 0;
                                      feature[i][j][8] = 1;
                                      break;
                                  case 9:feature[i][j][1] = feature[i][j][2] = feature[i][j][3] = feature[i][j][4] = feature[i][j][5] = feature[i][j][6] = feature[i][j][7] = feature[i][j][8] = 0;
                                      feature[i][j][9] = 1;
                                      break;
                                  }
                              }
                          }

                          for (i = 0;i < 9;i++)
                          {
                              for (j = 0;j < 9;j++)
                              {
                                  if (check[i][j] == 0)
                                  {
                                      for (r = 0; r < 9; r++)
                                      {
                                          for (y = 1;y <= 9;y++)
                                          {
                                              if (check[r][j] == y)
                                              {
                                                  feature[i][j][y] = 0;
                                              }
                                          }
                                      }
                                      for (c = 0; c < 9; c++)
                                      {
                                          for (y = 1;y <= 9;y++)
                                          {
                                              if (check[i][c] == y)
                                              {
                                                  feature[i][j][y] = 0;
                                              }
                                          }
                                      }
                                      for (k = ((int(i / 3)) * 3);k <= int((i / 3)) * 3 + 2;k++)
                                      {
                                          for (l = (int(j / 3)) * 3;l <= (int(j / 3)) * 3 + 2;l++)
                                          {
                                              for (y = 1;y <= 9;y++)
                                              {
                                                  if (check[k][l] == y)
                                                  {
                                                      feature[i][j][y] = 0;
                                                  }
                                              }
                                          }
                                      }
                                  }
                              }
                          }//特征值设定
                          for (b = 0;b <= 8;b++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                                  {
                                      for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                                      {
                                          sumbc[b][j][y] += feature[i][j][y];
                                          sumbr[b][i][y]+=feature[i][j][y];
                                          sumb[b][y] += feature[i][j][y];
                                      }

                                  }
                              }
                          }
                          for (b = 0;b <= 8;b++)
                          {
                              for (y = 1;y <= 9;y++)
                              {

                                      for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                                      {
                                          for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                                          {
                                              if (feature[i][j][y] == 1&& (sumb[b][y] == 1))
                                              {
                                                  check[i][j] = y;
                                              }
                                              if (sumbc[b][j][y] == sumb[b][y]&& sumb[b][y]!=0&& sumbc[b][j][y]!=0)
                                              {
                                                  for (r = 0;r < 9;r++)
                                                  {
                                                      if (r != (b / 3) * 3 && r!=(b / 3) * 3 + 1&&r!= (b / 3) * 3 + 2)
                                                      {
                                                          feature[r][j][y] = 0;
                                                      }
                                                  }
                                              }
                                              if (sumbr[b][i][y] == sumb[b][y]&& sumb[b][y]!=0&& sumbr[b][i][y]!=0)
                                              {
                                                  for (c = 0;c < 9;c++)
                                                  {
                                                      if (c !=(b % 3) * 3 && c!=(b % 3) * 3 + 1 && c!=(b % 3) * 3 + 2)
                                                      {
                                                          feature[i][c][y] = 0;
                                                      }
                                                  }
                                              }
                                          }
                                      }


                              }
                          }//一次宫摒除法*/
                          for (i = 0;i < 9;i++)
                          {
                              for (j = 0;j < 9;j++)
                              {
                                  for (y = 1;y <= 9;y++)
                                      sum[i][j] += feature[i][j][y];
                              }
                          }
                          for (i = 0;i < 9;i++)
                          {
                              for (j = 0;j < 9;j++)
                              {
                                  if (check[i][j] == 0)
                                  {
                                      if (sum[i][j] == 1)
                                      {
                                          for (y = 1;y <= 9;y++)
                                              check[i][j] += y * feature[i][j][y];
                                      }
                                  }
                              }
                          }//唯一余数法
                          for (i = 0;i < 9;i++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  for (j = 0;j < 9;j++)
                                  {
                                      sumr[i][y] += feature[i][j][y];
                                  }
                              }
                          }
                          for (i = 0;i < 9;i++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  if (sumr[i][y] == 1)
                                  {
                                      for (j = 0;j < 9;j++)
                                      {
                                          if (feature[i][j][y] == 1)
                                          {
                                              {check[i][j] = y;}
                                          }
                                      }
                                  }
                              }
                          }//行摒除法
                          for (j = 0;j < 9;j++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  for (i = 0;i < 9;i++)
                                  {
                                      sumr[j][y] += feature[i][j][y];
                                  }
                              }
                          }
                          for (j = 0;j < 9;j++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  if (sumr[j][y] == 1)
                                  {
                                      for (i = 0;i < 9;i++)
                                      {
                                          if (feature[i][j][y] == 1)
                                          {
                                              {check[i][j] = y;}
                                          }
                                      }
                                  }
                              }
                          }//列摒除法
                          for (b = 0;b <= 8;b++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                                  {
                                      for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                                      {

                                          sumb[b][y] += feature[i][j][y];
                                      }
                                  }
                              }
                          }
                          for (b = 0;b <= 8;b++)
                          {
                              for (y = 1;y <= 9;y++)
                              {
                                  if (sumb[b][y] == 1)
                                  {
                                      for (i = int(b / 3) * 3;i <= int(b / 3) * 3 + 2;i++)
                                      {
                                          for (j = (b % 3) * 3;j <= (b % 3) * 3 + 2;j++)
                                          {
                                              if (feature[i][j][y] == 1)
                                              {
                                                  check[i][j] = y;
                                              }

                                          }
                                      }
                                  }
                              }
                          }//二次宫摒除法

                      }

                              for (r = 0; r < 9; r++)
                              {
                                  for (c = 0; c < 9; c++)
                                  {

                                      if(check[r][c]!=0)
                                          _btn[r][c].setText(QString::number(check[r][c]));
                                  }
                              }
                              ui->showAnswerBtn->setEnabled(true);
              }else
              {
                  for(int a=0; a<81; a++)
                      _btn[a/9][a%9].setText("");
            srand((unsigned)(time)(NULL));//下面开始随机生成局面
            i = 10;//局面开始的固定数值10个
            while (count < i)
            {
                layer = rand() % 81;

                    rend = rand() % 9 + 1;  //随机生产一个数字（1-9）

                    row = layer / 9; //定位行值与列值
                    col  = layer % 9;

                    for (r = 0; r < 9; r++)
                        if (num[r][col] == rend)
                          {  goto randend; }//如果同列有相同数字了，继续循环，但count增加

                    for (c = 0; c < 9; c++)
                        if (num[row][c] == rend)
                            goto randend; //如果同行有相同数字了，那么同上

                    for (int kl = ((layer / 9) / 3) * 3; kl < ((layer / 9) / 3) * 3 + 3; kl++)
                        for (int kh = ((layer % 9) / 3) * 3; kh < ((layer % 9) / 3) * 3 + 3; kh++)
                           if (num[kl][kh] == rend)
                               goto randend; //如果九宫域有相同数字了，那么同上

                    flag[layer] = 1;
                    num[row][col] = rend; //把随机生产的数字放入数组中
                    count++;

            randend:;
            }
            //到这里局面已生成
            i = r = c = rend = cend = back = layer = row = col = count = 0; //变量清零，方便后来的算法思考


            while (layer < 81) //因为要测试81次，layer代表81层的每一层
            {
                if (1 == flag[layer]) //这个分支用来测试是否计算到了固定数值，如果是，则跳过这一块
                {
                    if (back == 1) { goto no_match; } //如果正处于回溯状态，就当成无满足条件的递归式回到上一层
                    layer++; // 不然则当成前进式计算下一层
                    continue;
                }

                row = layer / 9; col = layer % 9; //把这一层用来转换定位行和列的具体坐标

                for (i = backarr[layer][back] + 1; i < 10; i++) //注意那个backarr，它用来当作测试数值的下限值
                {
                    for (c = 0; c < 9; c++)//检查横列
                    if (c >= col)
                    {
                        if (flag[row * 9 + c] == 1 && num[row][c] == i)  goto end;//i不符合条件，刷新i
                    }
                    else if (num[row][c] == i)  { goto end; }

                    for (r = 0; r < 9; r++) //检查竖列
                    if (r >= row)
                    {
                        if (flag[r * 9 + col] == 1 && num[r][col] == i)  goto end;
                    }
                    else if (num[r][col] == i)  goto end;

                    rend = ((row / 3) * 3) + 3; cend = ((col / 3) * 3) + 3; //检查九宫格
                    for (r = (row / 3) * 3; r < rend; r++)
                    for (c = (col / 3) * 3; c < cend; c++)
                    if ((r * 9 + c) >= layer)
                    {
                        if (flag[r * 9 + c] == 1 && num[r][c] == i)  goto end;
                    }
                    else if (num[r][c] == i)  goto end;


                    goto match_go;

                end:continue;
                }
                goto no_match;

            match_go:
                num[row][col] = i; //i暂时符合限制条件，程序继续前进式计算
                backarr[layer][1] = i; //设置这一层的下限值，不然回溯时算法会进入死循环
                back = 0;
                layer++;
                continue;

            no_match:
                back = 1; //没有符合条件的数值，将状态职位回溯
                layer--;
            }
            //程序运行到这里表示已经找到解答了
            srand((unsigned)(time)(NULL));
            while (isonly(num) == 1)//用解数独函数判断是否可解
            {
                row = rand() % 9;
                col = rand() % 9;
                w = num[row][col];
                num[row][col] = 0;
            }
            num[row][col] = w;//对完整数独进行挖空，直至没有唯一解。
}




    for (r = 0; r < 9; r++)
             {
                 for (c = 0; c < 9; c++)
                 {
                     if(num[r][c]!=0)
                         _btn[r][c].setText(QString::number(num[r][c]));
                 }
             }
ui->createEnigmaBtn->setEnabled(true);


        }

void Widget::setNum(int row, int col, int num)//用num数字，设置为row行col列块的数字
{
    int value = num+1;
    for(int c=0; c<9; c++)
        if(value == _btn[row][c].text().toInt())
        {
            QMessageBox::warning(0,tr("警告"),tr("在同行中有重复数字，请检查！"));
            return;
        }
    for(int r=0; r<9; r++)
        if(value == _btn[r][col].text().toInt())
        {
            QMessageBox::warning(0,tr("警告"),tr("在同列中有重复数字，请检查！"));
            return;
        }
    for(int r=0; r<3; r++)
        for(int c=0; c<3; c++)
            if(value == _btn[row/3*3+r][col/3*3+c].text().toInt())
            {
                QMessageBox::warning(0,tr("警告"),tr("在同九宫域中有重复数字，请检查！"));
                return;
            }
    _btn[row][col].setText(QString::number(value));
}
void Widget::clearNum(int row, int col) //擦除这一个块的数字
{
    _btn[row][col].setText("");
}

void Widget::on_showAnswerBtn_clicked()
{
    ui->showAnswerBtn->setEnabled(false);
    this->process(1);
}

void Widget::on_createEnigmaBtn_clicked()
{
    ui->createEnigmaBtn->setEnabled(false);
    this->process(0);
}

void Widget::on_clearAllBtn_clicked()
{
    for(int row=0; row<9; row++)
        for(int col=0; col<9; col++)
            _btn[row][col].setText("");
}



